<script type="text/javascript">
    $(document).ready(function() {
        // Toggle month panel
        $.fn.togglemonthpanels = function(){
            return this.each(function(){
                $(this).addClass("monthaccordion")
                    .find("p.month-section")
                    //.addClass("ui-accordion-header ui-helper-reset ui-state-default ui-corner-top ui-corner-bottom")
                    .addClass("monthaccordion-header monthaccordion-state-default")
                    .hover(function() { $(this).toggleClass("monthaccordion-state-hover"); })
                    //.prepend('<span class="ui-icon ui-icon-triangle-1-e"></span>')
                    .click(function() {
                        $(this)
                            //.toggleClass("ui-accordion-header-active ui-state-active ui-state-default ui-corner-bottom")
                            .toggleClass("monthaccordion-header-active monthaccordion-state-active monthaccordion-state-default")
                            //.find("> .ui-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s").end()
                            .next().slideToggle();
                        return false;
                    })
                    .next()
                    .addClass("monthaccordion-content")
                    .hide();
            });
        };
        $(".monthpanel").togglemonthpanels();

        // Toggle week panel
        $.fn.toggleweekpanels = function(){
            return this.each(function(){
                $(this).addClass("weekaccordion")
                    .find("p.week-section")
                    //.addClass("ui-accordion-header ui-helper-reset ui-state-default ui-corner-top ui-corner-bottom")
                    .addClass("weekaccordion-header weekaccordion-state-default")
                    .hover(function() { $(this).toggleClass("weekaccordion-state-hover"); })
                    //.prepend('<span class="ui-icon ui-icon-triangle-1-e"></span>')
                    .click(function() {
                        $(this)
                            //.toggleClass("ui-accordion-header-active ui-state-active ui-state-default ui-corner-bottom")
                            .toggleClass("weekaccordion-header-active weekaccordion-state-active weekaccordion-state-default")
                            //.find("> .ui-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s").end()
                            .next().slideToggle();
                        return false;
                    })
                    .next()
                    .addClass("weekaccordion-content")
                    .hide();
            });
        };
        $(".weekpanel").toggleweekpanels();
    });
</script>
<table>
    <tr>
        <td class="tabs-synthese-top">
            <table border="1">
                <tr>
                    <th class="hide mo">MO</th>
                    <th class="popr">Pertes</th>
                    <th class="popr">Offerts</th>
                    <th class="popr">Repas</th>
                    <th class="hide bq">Banques</th>
                    <th class="hide tsf">Transferts</th>
                    <th class="hide ac">Annulation caisse</th>
                    <th class="hide cc">Compte clients</th>
                    <th class="tv">Tx de prise</th>
                    <th class="ca">CA TTC</th>
                    <th class="ca">CA HT</th>
                    <th class="mt">Météo S/N/P</th>
                    <th class="mt">T°</th>
                    <th class="ec">Ecarts caisse</th>
                    <th class="tv">Nb d'articles</th>
                    <th class="tv">Transactions</th>
                    <th class="tm">Ticket moyen</th>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <?php foreach ($period as $idxm => $month) { ?>
            <div class="monthpanel">                                
                <p class="month-section"><span class="icons"></span><?php echo $idxm; ?></p>
                <div>
                    <?php 
                    $weekCount = 1;
                    foreach ($month as $week) { ?>
                    <div class="weekpanel">
                        <p class="week-section"><span class="icons"></span>Semaine <?php echo $weekCount; ?></p>
                        <div>
                            <table class="day-section">
                            <?php foreach($week as $idxd => $day) { 
                                $ca_ttc = $ca_ht = 0;
                                $dsd_class = "day-section-date";
                                $dsv_class = "";

                                // Get CA for each day
                                foreach($ca as $ca_day):
                                    if($day->format('Y-m-d')==$ca_day['orderDate']){
                                        $ca_ttc = $ca_day['ca_ttc'];
                                        $ca_ht = $ca_day['ca_ht'];
                                    }
                                endforeach;

                                // Get TM for each day
                                foreach($tm as $tm_day):
                                    if($day->format('Y-m-d')==$tm_day['orderDate']){
                                        $tm_ttc = ($tm_day['nombre']!=0)?($tm_day['valeur']/$tm_day['nombre']):0;
                                    }
                                endforeach;

                                // Get nbT for each day
                                foreach($nbT as $nbT_day):
                                    if($day->format('Y-m-d')==$nbT_day['orderDate']){
                                        $nbTra = ($nbT_day['nbTransaction']!=0)?$nbT_day['nbTransaction']:0;
                                    }
                                endforeach;
                                
                                // Get nbA for each day
                                foreach($nbA as $nbA_day):
                                    if($day->format('Y-m-d')==$nbA_day['orderDate']){
                                        $nbArt = ($nbA_day['nbArticle']!=0)?$nbA_day['nbArticle']:0;
                                    }
                                endforeach;
                                
                                // Get meteo for each day
                                $meteo = "";
                                $temperature = "";
                                foreach($meteoDays as $meteoDay){                                                                        
                                    if($day->format('Y-m-d')==$meteoDay['meteoDate']){                                        
                                        $meteo = $meteoDay['type'];
                                        $temperature = number_format($meteoDay['temperature'],1,',', ' ');
                                    }
                                }
                                
                                // Get ecarts caisse for each day
                                foreach($ecDays as $ec_day):
                                    if($day->format('Y-m-d')==$ec_day['date']){
                                        $ec = ($ec_day['ec']!=0)?$ec_day['ec']:0;
                                    }
                                endforeach;
                                
                                // Get meal count for each day
                                $repas = 0;
                                foreach($repasDays as $repas_day):
                                    if($day->format('Y-m-d')==$repas_day['orderDate']){
                                        $repas = ($repas_day['repas']!=0)?$repas_day['repas']:0;
                                    }
                                endforeach;

                                if($idxd == "dimanche") {
                                    $dsd_class .= " day-section-sunday";
                                    $dsv_class .= " day-section-sunday";
                                }

                                foreach($ca as $ca_day):
                                    if($day->format('Y-m-d')==$ca_day['orderDate']){
                                ?>
                                <tr>
                                    <td class="<?php echo $dsd_class; ?>" style="width:52px"><?php echo $idxd; ?></td>
                                    <td class="<?php echo $dsd_class; ?>" style="width:63px"><?php echo $day->format('d-M-y'); ?></td>
                                    <td style="padding: 0 0 0 2px;">
                                        <table border="1">
                                            <tr>
                                                <td class="day-section-value hide mo<?php echo $dsv_class; ?>">
                                                    <span></span>
                                                </td>
                                                <td class="day-section-value popr<?php echo $dsv_class; ?>">
                                                    <span></span>
                                                </td>
                                                <td class="day-section-value popr<?php echo $dsv_class; ?>">
                                                    <span></span>
                                                </td>
                                                <td class="day-section-value popr <?php echo $dsv_class; ?>">
                                                    <span><?php echo number_format($repas,0,',', ' '); ?></span>
                                                </td>
                                                <td class="day-section-value hide bq <?php echo $dsv_class; ?>">
                                                    <span></span>
                                                </td>
                                                <td class="day-section-value hide tsf <?php echo $dsv_class; ?>">
                                                    <span></span>
                                                </td>
                                                <td class="day-section-value hide ac <?php echo $dsv_class; ?>">
                                                    <span></span>
                                                </td>
                                                <td class="day-section-value hide cc <?php echo $dsv_class; ?>">
                                                    <span></span>
                                                </td>
                                                <td class="day-section-value tv<?php echo $dsv_class; ?>">
                                                    <span><?php echo number_format(($nbArt/$nbTra),2,',', ' '); ?></span>
                                                </td>
                                                <td class="day-section-value ca<?php echo $dsv_class; ?>">
                                                    <span><?php echo number_format($ca_ttc,2,',', ' '); ?></span>
                                                </td>
                                                <td class="day-section-value ca<?php echo $dsv_class; ?>">
                                                    <span><?php echo number_format($ca_ht,2,',', ' '); ?></span>
                                                </td>
                                                <td class="day-section-value mt<?php echo $dsv_class; ?>">
                                                    <span><?php echo $meteo; ?></span>
                                                </td>
                                                <td class="day-section-value mt<?php echo $dsv_class; ?>">
                                                    <span><?php echo $temperature; ?></span>
                                                </td> 
                                                <td class="day-section-value ec<?php echo $dsv_class; ?>">
                                                    <span><?php echo number_format($ec,2,',', ' '); ?></span>
                                                </td>
                                                <td class="day-section-value tv<?php echo $dsv_class; ?>">
                                                    <span><?php echo number_format($nbArt,0,',', ' '); ?></span>
                                                </td>
                                                <td class="day-section-value tv<?php echo $dsv_class; ?>">
                                                    <span><?php echo number_format($nbTra,0,',', ' '); ?></span>
                                                </td>
                                                <td class="day-section-value<?php echo $dsv_class; ?>">
                                                    <span><?php echo number_format($tm_ttc,2,',', ' '); ?></span>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>                                                                
                                </tr>
                                <?php } endforeach; } ?>
                            </table>
                        </div>
                    </div>
                    <?php $weekCount++; } ?>
                </div>                                
            </div>
            <?php } ?>
        </td>
    </tr>
</table>
<?php //var_dump($repasDays); ?>